//
//  CommentsNoticeVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/16.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "CommentsNoticeVC.h"
#import "CommentCell.h"
#import "CommentModel.h"
#import "TableViewAnimationKit.h"

@interface CommentsNoticeVC ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *_data;
}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation CommentsNoticeVC


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavTitle:@"评论通知" titleColor:[UIColor blackColor] font:font17];
    self.tableView.backgroundColor = self.view.backgroundColor;
    
    UIView *heder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 20)];
    heder.backgroundColor = self.view.backgroundColor;
    self.tableView.tableHeaderView = heder;
    _data = @[];
    [self loadingData];
    
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadingData];
        
    }];
    //当然有评论推送过来时，刚好在评论页面，直接刷新
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(againLoading) name:@"news_CommentList" object:nil];
    

}
//减少角标数字
- (void)toreduce{
    NSDictionary *dic = [userDefaults objectForKey:@"NocationList"];
    NSMutableArray *arr = dic.mutableCopy;
    
    NSMutableDictionary *newdic = ((NSDictionary *)arr[0]).mutableCopy;
    if([newdic[@"newItem"] intValue]>0){
        [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Notice/unreadChange" isactive:YES parameter:@{@"number":@"0",@"flag":@"comment"} successBlock:^(PPHttpRequest *request, id responseObj) {
            newdic[@"newItem"] = [NSString stringWithFormat:@"%d",[responseObj[@"comment"] intValue]];
            arr[0] = newdic;
            [userDefaults setObject:arr forKey:@"NocationList"];
        } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
            [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        }];
    }
}


- (void)againLoading{
    [self.tableView.mj_header beginRefreshing];
}

//加载全部评论数据，由于数据比较少，占时没有分页
- (void)loadingData{
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Notice/commentNotice" parameter:@{} successBlock:^(PPHttpRequest *request, id responseObj) {
        NSMutableArray *dataArray = @[].mutableCopy;
        [((NSArray *)responseObj) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CommentModel *mode = [CommentModel yy_modelWithJSON:responseObj[idx]];
            [dataArray addObject:mode];
        }];
        _data = dataArray;
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
        [weakself toreduce];
        [TableViewAnimationKit toTopAnimationWithTableView:self.tableView];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
         [weakself.tableView.mj_header endRefreshing];
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}

- (UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, SCREEN_W, SCREEN_H-TabBarItemHeight-NavigationHeight+69) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:@"CommentCell"];
        //UITableView去掉自带系统的分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentModel *dataModel = _data[indexPath.row];
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColorFromRGB(0xEBEBEB);
   
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.smallTime.text = [NSString stringWithFormat:@"%@",[ToolClass strToTime:dataModel.add_time format:@"yyyy-MM-dd hh:mm:ss"]];
    cell.contentLab.text = [NSString stringWithFormat:@"%@",dataModel.content];
    cell.userName.text = [NSString stringWithFormat:@"%@",dataModel.nickname];
    cell.pfLab.text = [NSString stringWithFormat:@"%@ 分",dataModel.star];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentModel *dataModel = _data[indexPath.row];
    
    NSString *goodsname = dataModel.content;
    CGSize titleSize = [goodsname boundingRectWithSize:CGSizeMake(SCREEN_W-50, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16] } context:nil].size;
    
    return titleSize.height + 80;
}

@end
